package com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.screen.proof.subview

import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.View.OnFocusChangeListener
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import com.helasasa.maxfunds.lineofcredit.cashloan.R
import org.json.JSONObject

class ProofCameroonInput : LinearLayout {

    private lateinit var title: TextView
    private lateinit var input: EditText
    private lateinit var inputContainer: LinearLayout

    private var bindData: JSONObject? = null

    constructor(context: Context) : super(context) {
        initializeView(context)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initializeView(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initializeView(context)
    }

    private fun initializeView(context: Context) {
        val rootView = inflate(context, R.layout.view_cameroon_proof_input, this)
        orientation = VERTICAL
        title = rootView.findViewById(R.id.title)
        input = rootView.findViewById(R.id.proofInput)
        inputContainer = rootView.findViewById(R.id.inputContainer)

        input.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                // 监听输入内容变化，可以在这里处理相应的逻辑
                val inputText = s.toString()
                // 这里可以执行监听到输入内容变化后的逻辑操作
                try {
                    // update bind data
                    bindData?.put("content", inputText)
                    bindData?.put("realContent", inputText)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        })

        input.onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
            if (!hasFocus) {
                // 输入框失去焦点时的处理逻辑
            }
        }
    }

    fun setBindData(jsonObject: JSONObject?) {
        this.bindData = jsonObject
        setTitle(jsonObject?.optString("title"))
        setInputHint(jsonObject?.optString("hint"))
        setInputText(jsonObject?.optString("content"))
    }

    fun setTitle(title: String?) {
        this.title.text = title
    }

    fun setInputHint(hint: String?) {
        input.hint = hint
    }

    fun setInputText(text: String?) {
        input.setText(text)
    }

    fun getInputText(): String {
        return input.text.toString()
    }
}